package com.jf.template.stat.ast;

import java.io.Writer;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jf.template.Env;
import com.jf.template.expr.ast.ExprList;
import com.jf.template.stat.Scope;
import com.wbcs.config.component.application.report.ConditionBean;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.WbcsResponse;
import com.wbcs.system.assistant.ComponentAssistant;
import com.wbcs.system.assistant.TagAssistant;
import com.wbcs.util.Consts;
import com.wbcs.util.Consts_Private;

public class Condition extends Stat{

	private ExprList exprList;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public Condition(ExprList exprList, HttpServletRequest request, HttpServletResponse response){
		this.exprList = exprList;
		this.request = request;
		this.response = response;
	}
	public void exec(Env env, Scope scope, Writer writer) {
		ReportRequest rrequest;
		WbcsResponse wresponse;
		Object value = exprList.eval(scope);
		String paramArr[] = null;
		Map<String,String> paraMap = null;
		boolean isAllCondition = false;
		if (value != null) {
			if(value instanceof Map){
				paraMap = ((Map<String,String>)value);
				for(String key : paraMap.keySet()){
					isAllCondition = true;
					paramArr = paraMap.get(key).split("\\.");break;
				}
			}else if(value instanceof String){
				paramArr = value.toString().split("\\.");
			}
			
			if(request.getAttribute(Consts_Private.REPORT_TEMPLATE) == null){
				rrequest=new ReportRequest(request,Consts.DISPLAY_ON_PAGE);
				wresponse=new WbcsResponse(response);
				rrequest.setWResponse(wresponse);
	            wresponse.setRRequest(rrequest);
	            request.setAttribute(Consts_Private.REPORT_TEMPLATE, rrequest);
				try{
					rrequest.init(paramArr[0]);
				}catch(Exception e){
					e.printStackTrace();
				}finally{
					rrequest.destroy(true);//占用资源释放
				}
			}else{
				rrequest = (ReportRequest) request.getAttribute(Consts_Private.REPORT_TEMPLATE);
				wresponse = rrequest.getWResponse();
			}
            for(ReportBean rBean : rrequest.getLstAllReportBeans()){
            	if(rBean.getId().equals(paramArr[1])){
            		String conditionStr = "";
            		if(isAllCondition){
            			for(String key : paraMap.keySet()){
            				String name = paraMap.get(key).split("\\.")[2];
            				ConditionBean cBean = rBean.getSbean().getConditionBeanByName(name);
            				conditionStr += cBean.getDisplayString(rrequest,null,null,true,-1);
            				if(cBean.isBr()){
            					conditionStr += "<br/>";
            				}
            			}
            			conditionStr = "<div class='cls-condition-box'>"+conditionStr+"<button class='el-button cls-button-search' v-on:click='search'><i class='el-icon-search'></i></button></div>";
            		}else{
            			conditionStr = rBean.getSbean().getConditionBeanByName(paramArr[2]).getDisplayString(rrequest,null,null,true,-1);
            		}
            		write(writer, conditionStr.replaceAll("(onkeypress|onfocus|onblur|onmouseover|onmouseout)=\"[^\"]*?\"", ""));
            		break;
            	}
            }
		}
	}
	public void exec(Env env, Scope scope, Writer writer, HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		exec(env,scope,writer);
	}
}
